<template>
  <div class="edit-wrap">
    <x2-form ref="editform" :is-new="isNewRecord" :form-config="formConfig"></x2-form>
    <div class="bottom-btns">
      <x2-space></x2-space>
      <el-button @click="handleSubmit" type="primary">保存提交</el-button>
      <el-button @click="$emit('close')" type="warning">取消返回</el-button>
    </div>
  </div>
</template>

<script>
import { dictFormConfig } from './config'
export default {
  name: 'DictEdit',
  data() {
    return {
      formConfig: dictFormConfig,
      isNewRecord: false,
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      if (this.$attrs.id) {
        const res = await this.$request({
          url: '/lowcode/getDataBySql',
          method: 'post',
          data: { sqlid: '字典键值明细', where: `id = '${this.$attrs.id}'` },
        })

        const row = res.list[0]
        setTimeout(() => {
          this.$refs.editform.init(row)
        }, 0)

        // console.log('DictMainEdit > init > res', res)
      } else {
        this.isNewRecord = true
        setTimeout(() => {
          this.$refs.editform.init({
            id: this.$uuid(),
            mid: this.$attrs.mid,
            dtext: '',
            dvalue: '',
          })
        }, 0)
      }
    },
    async handleSubmit() {
      const res = await this.$refs.editform.getData()
      if (res) this.$emit('confirm', res)
    },
  },
}
</script>
<style scoped>
.edit-wrap {
  /* width: 200px; */
}
.bottom-btns {
  display: flex;
  align-items: center;
}
</style>
